# -*- coding:utf-8 -*-

from selenium import webdriver
import time
from bs4 import BeautifulSoup
import threading
import requests
from lxml import etree
import re

pingbici=['您好', '感谢您', '谢谢', '关注', '披露', '公告', '资讯网', '谢谢您', '巨潮', '公司公告',
          '为准', 'http', 'www', 'cninfo', 'com', 'cn', '指定', '事项', '相关', '对本', '及时', '投资者',
          '查阅', '尊敬', '信息', '建议', '支持', '证券时报', '回复', '定期', '证券报', '请参阅', '总户数',
          '宝贵意见', '媒体', '请以', '2016', 'XX', '复牌', '股东', '报告', '发布', '有关', '敬请', '详情',
          '互动', '参见', '感谢', '后续','请关注','后续相关公告','请关注公司公告','尊敬的投资者','感谢','谢谢',
          '对公司的关注','根据','有关规定','及时披露','《中国证券报》','《证券时报》','巨潮资讯网','重视股东投资回报',
          '关注股价表现','提醒投资者','谨慎对待','证监会指定的信息披露媒体','发布的公告为准','会及时发布相关公告','您好',
          '互动问题','给予回复','谢谢关注','官方信息为准','谢谢','参见之前回复','您的建议','感谢','对公司的关注和建议',
          '感谢您的关注与支持','您好','请参考公司定期报告','尊敬的投资者','有关此次停牌事项','请查阅公司如有',
          '相关重大事项','会及时公告','关注公司公告','非常感谢','宝贵意见','已披露的公告',
          '查阅公司定期报告','敬请关注','尊敬的投资者','你好','应披露','未披露','积极关注',
          '真实、准确、完整，不存在虚假记载、误导性陈述或重大遗漏','法律责任','提醒广大投资者','不代表公司观点','法定信息披露媒体','请勿轻信','谨慎判断',]

#屏蔽词列表

answerbox=[]

stockbox=[]


url='http://irm.cninfo.com.cn/ircs/sse/sseSubIndex.do?condition.type=7'
ans_url='http://irm.cninfo.com.cn/ircs/interaction/'

driver=webdriver.PhantomJS()

answer_revised=[]
freq_str = ''
Elite_answer_list=[]

#存放所有条目回复链接
all_answer=[]
Elite_all_answer=[]

class HDY():
    def __init__(self):
        driver.set_window_size(1920, 2000)
        driver.get(url)

        # 切换至对应的frame页面
        driver.switch_to.frame(1)

        time.sleep(1)


    # 获取全部最新回复

    def answercontent(self,answer_revised):
        global date, stockname, askcontent,all_answer


        #抓取所有50页数据

        for i in range(3):

            soup=BeautifulSoup(driver.page_source,'lxml')
            items=soup.find_all(class_='msgBox')

            #抓取互动易内容
            for item in items:
                link = item.find(class_='cntcolor').get('href')
                link = ans_url+link

                req = requests.get(link)
                html = req.text


                tree = etree.HTML(html)
                Q = tree.xpath('//li//div[@class="msgBox"]')  # 提问
                result = tree.xpath('//li//div[@class="answerBox"]')

                for ask in Q:
                    askcontent = ask.xpath('./div[@class="msgCnt cntcolor"]//text()|./text()')

                    for answer in result:
                        stockname = answer.xpath('./div[@class="msgCnt cntcolor"]/a/text()')

                        date = answer.xpath('./div[@class="pubInfo2"]//text()')
                        content = answer.xpath('./div[@class="msgCnt cntcolor"]/text()|./div/text()')

                        answer_revised = "".join(content[1].split()).replace(':', '').replace('答：', '')
                        answer_list = re.split(r'(；|。|，|！)', answer_revised)


                        if "".join(content[1].split()).replace(':', '') == '':
                            pass
                        else:

                            for i in answer_list:
                                if i == '；' or i == '。' or i == '，' or i == '！':
                                    answer_list.remove(i)
                                    answer_revised = '，'.join(answer_list)
                            if len(answer_revised) > 0:
                                sentence = date[3] + '\n' + stockname[0] + '\n' + '问：' + (
                                    askcontent[8] + askcontent[9]).strip() + '\n' + stockname[
                                               0] + '在互动平台上表示，' + answer_revised.strip(
                                    '，') + '。'
                                answerbox.append(sentence)

                                code = stockname[0]
                                asktext = '问：' + (askcontent[8] + askcontent[9]).strip()
                                answertext = stockname[0] + '在互动平台上表示，' + answer_revised.strip('，') + '。'
                                date = date[3]

                                if [code, asktext, answertext, date] not in all_answer:
                                    all_answer.append([code, asktext, answertext, date])

            driver.find_element_by_link_text('>').click()
            time.sleep(1)
        return all_answer

# 获取全部最新精华回复继承类
class Elite_HDY(HDY):


    def answercontent(self, answer_revised):

        global date, stockname, askcontent,all_answer,freq_str,stockbox

        # 抓取所有50页数据

        for i in range(50):
            soup = BeautifulSoup(driver.page_source, 'lxml')
            items = soup.find_all(class_='msgBox')

            # 抓取互动易内容
            for item in items:
                link = item.find(class_='cntcolor').get('href')
                link = ans_url + link

                req = requests.get(link)
                html = req.text

                tree = etree.HTML(html)
                Q = tree.xpath('//li//div[@class="msgBox"]')  # 提问
                result = tree.xpath('//li//div[@class="answerBox"]')

                for ask in Q:
                    askcontent = ask.xpath('./div[@class="msgCnt cntcolor"]//text()|./text()')

                    for answer in result:
                        stockname = answer.xpath('/html/body/div[2]/div[1]/div/div/ul/li/div[1]/div[3]/div[1]/div/a[2]/text()')

                        date = answer.xpath('./div[@class="pubInfo2"]//text()')
                        content = answer.xpath('./div[@class="msgCnt cntcolor"]/text()|./div/text()')

                        answer_revised = "".join(content[1].split()).replace(':', '').replace('答：', '')
                        answer_list = re.split(r'(；|。|，|！)', answer_revised)

                        if "".join(content[1].split()).replace(':', '') == '':
                            pass
                        else:
                            for word in pingbici:
                                for i in answer_list:
                                    if word in i or i == '；' or i == '。' or i == '，' or i == '！':
                                        answer_list.remove(i)
                                        answer_revised = '，'.join(answer_list)

                            if len(answer_revised) > 150:
                                sentence = date[3] + '\n' + stockname[0] + '\n' + '问：' + (
                                    askcontent[8] + askcontent[9]).strip() + '\n' + stockname[
                                               0] + '在互动平台上表示，' + answer_revised.strip(
                                    '，') + '。'
                                answerbox.append(sentence)

                                code = stockname[0]
                                asktext = '问：' + (askcontent[8] + askcontent[9]).strip()
                                answertext = stockname[0] + '在互动平台上表示，' + answer_revised.strip('，') + '。'
                                date = date[3]

                                print(code)
                                print(asktext)
                                print(answertext)
                                print(date)
                                print('=====================================')

                                # 获取互动精华个股代码列表
                                if code[-7:-1] not in stockbox:
                                    stockbox.append(code)


                                if [code, asktext, answertext, date] not in Elite_all_answer:
                                    Elite_all_answer.append([code, asktext, answertext, date])

                                freq_str+=answer_revised.strip('，')


            driver.find_element_by_link_text('>').click()
            time.sleep(1)

        return Elite_all_answer,stockbox,freq_str
